package objects
{
	import flash.events.Event;
	import flash.geom.Point;
	import objects.cicleItem.cicleItemBaseBuild;
	import objects.UpdateStarItem.cicleItemUpdateStar;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	/**
	 * ...
	 * @author ...Vượng
	 */
	public class updateStarListItem extends MyObject
	{
		public var m_cicleItem:cicleItemUpdateStar;
		public var m_testChoose:Boolean;
		public var m_sprite:Sprite;
		public var m_testBuy:Boolean;
		public var resule:int;
		public var m_base:String;
		
		public function updateStarListItem(_sprite:Sprite, _pos:Point, _type:int, _base:String, _itemIndex:Array)
		{
			m_sprite = _sprite;
			m_base = _base;
			m_pos = new Point(_pos.x, _pos.y);
			m_cicleItem = new cicleItemUpdateStar(this, m_pos, _type, _itemIndex);
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			m_sprite.addEventListener(TouchEvent.TOUCH, onTouch);
			m_sprite.addChild(this);
			m_cicleItem.m_result = -1;
		
		}
		
		public function levelUp(starTotal:int, starNeed:int):int
		{
			if (starNeed <= starTotal)
			{
				m_cicleItem.levelUp();
				starTotal -= starNeed;
				return starTotal;
			}
			return starTotal;
		}
		
		private function onTouch(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);
			if (touch)
			{
				var _pointTouch:Point = new Point(touch.globalX, touch.globalY);
				if (touch.phase == TouchPhase.ENDED)
				{
					m_cicleItem.m_result = -1;
					for (var i:int = 0; i < m_cicleItem.m_baseItem.length; i++)
					{
						if (m_cicleItem.m_baseItem[i].bounds.containsPoint(_pointTouch) == true)
						{
							if (database.instance.m_testPlaySound == true)
								database.instance.m_Sound.playSound("Sound_Click");
							m_cicleItem.m_result = i;
						}
					}
				}
				else if (touch.phase == TouchPhase.HOVER)
				{
					m_cicleItem.blurFilter(_pointTouch);
				}
			}
		}
		
		override public function update(dt:Number):void
		{
		
		}
		
		private function init(e:starling.events.Event):void
		{
			removeEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			//anh nen
			m_spriteImage = new Image(Assets.getSpriteBook().getTexture(m_base));
			this.addChild(m_spriteImage);
			m_spriteImage.visible = true;
			
			this.setAnchorPoint();
			this.setPos(m_pos);
		}
		
		public function setAnchorPoint():void
		{
			m_spriteImage.pivotX = m_spriteImage.width / 2;
			m_spriteImage.pivotY = m_spriteImage.height / 2;
		}
		
		public function setPos(_pos:Point):void
		{
			m_spriteImage.x = _pos.x;
			m_spriteImage.y = _pos.y;
		}
		
		public function setScale(_scale:Number):void
		{
			m_spriteImage.scaleX = _scale;
			m_spriteImage.scaleY = _scale;
		}
		
		public function releaseFull(_listContain:Array, _index:int):void
		{
			this.removeChildren();
			m_cicleItem.release();
			m_sprite.removeChild(this);
			_listContain.splice(_index, 1);
		}
	}

}